/*
 * Copyright (c) 2020. bmy
 */

import 'reflect-metadata';
import 'babel-polyfill'
// import '@types/RouterHooks';
import Vue, { VueConstructor, Component } from 'vue';
import App from '@application/App'
import { ConfigureConfig } from "@config/configure.config";
import NavigationGuards from "@utils/navigation-guards.utils";
import VueRouter, { RawLocation, Route } from 'vue-router';
import Vuex, { Store } from 'vuex'
import StoreModeule from '@store/index'
import { RegServiceAndMethods, RegVuePlugs, Start } from "@ann/register.annotation";
import { Autowired } from "@ann/ioc.annotation";

@Start
@RegVuePlugs
@RegServiceAndMethods
export class InitRun {

  @Autowired()
  protected NavigationGuards!: NavigationGuards;

  protected Router!: VueRouter;
  protected VueX!: Store<typeof StoreModeule>;
  protected app: Component = App;

  constructor() {
    Vue.config.productionTip = false;
    this.VueRouterAndVuex()
  }

  private VueRouterAndVuex(): void {
    this.Router = new VueRouter(ConfigureConfig.RouterConfigUrl);
    // this.NavigationGuards.beforeEach(this.Router)
    // this.NavigationGuards.afterEach(this.Router)

    // 重写路由Push
    const routerPush: (location: RawLocation) => Promise<Route> = VueRouter.prototype.push;
    VueRouter.prototype.push = function push(location: RawLocation): Promise<Route> {
      // @ts-ignore
      return routerPush.call(this, location).catch((error: Error) => error)
    };

    this.VueX = new Vuex.Store(StoreModeule)
  }
}

new InitRun();
